package com.ruoyi.web.controller.system;

import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.service.ProjectService;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HtProject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 *
 * @author zhz
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/system/project")
@Anonymous
public class HtProjectController extends BaseController {
    @Autowired
    private ProjectService projectService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(HtProject htProject) {
        startPage();
        List<HtProject> list = projectService.selectHtProjectList(htProject);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HtProject htProject) {
        List<HtProject> list = projectService.selectHtProjectList(htProject);
        ExcelUtil<HtProject> util = new ExcelUtil<HtProject>(HtProject.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(projectService.selectHtProjectById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HtProject htProject) {
        return toAjax(projectService.insertHtProject(htProject));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HtProject htProject) {
        return toAjax(projectService.updateHtProject(htProject));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(projectService.deleteHtProjectByIds(ids));
    }


    /**
     * 生成授权文件
     */
    @GetMapping(value = "/generaLicense/{id}/{endTime}/{license}")
    public ResponseEntity<Resource> generaLicense(@PathVariable("id") String id, @PathVariable("endTime") Long endTime, @PathVariable("license") String license) {
        Object o = projectService.generaLicense(id, endTime, license);
        byte[] contentBytes = o.toString().getBytes(StandardCharsets.UTF_8);
        ByteArrayResource resource = new ByteArrayResource(contentBytes);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "license.key");
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }

}
